package cn.llming.auth.service.impl;

import cn.llming.auth.service.ILoginService;
import cn.llming.dto.LoginDto;
import cn.llming.org.domain.Employee;
import cn.llming.org.mapper.EmployeeMapper;
import cn.llming.vo.result.BaseMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;


@Service
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Map<String, Object> login(LoginDto dto) {
        String username = dto.getUsername();
        String password = dto.getPassword();
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            throw new RuntimeException("账号或密码不能为空，重新输入");
        }
        Employee employee=employeeMapper.seleteLoginByName(username);
        if(Objects.isNull(employee)){
            throw new RuntimeException("账号或密码错误");
        }
        if(!password.equals(employee.getPassword())){
            throw new RuntimeException("账号或密码错误");
        }


        String token = UUID.randomUUID().toString();
        BaseMap.resultMap.put(token,employee);
        HashMap<String, Object> map = new HashMap<>();
        map.put("token", token);
        employee.setPassword("");
        map.put("userInfo", employee);

        return map;
    }
}
